package org.example.domain.strategy.service.raffle;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.example.domain.strategy.model.entity.RaffleAwardEntity;
import org.example.domain.strategy.model.entity.RaffleFactorEntity;
import org.example.domain.strategy.model.entity.RuleActionEntity;
import org.example.domain.strategy.model.entity.StrategyRuleEntity;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.repository.IStrategyRuleRepository;
import org.example.domain.strategy.service.IRaffleStrategy;
import org.example.domain.strategy.service.armory.IStrategyArmory;
import org.example.domain.strategy.service.armory.IStrategyDispatch;
import org.example.domain.strategy.service.rule.chain.ILogicChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.domain.strategy.service.rule.factory.DefaultLogicFactory;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

public abstract class AbstractRaffleStrategy implements IRaffleStrategy {
    @Resource
    IStrategyRuleRepository strategyRuleRepository;
    @Resource
    IStrategyDispatch strategyDispatch;

    @Resource
    DefaultChainFactory defaultChainFactory;

    @Override
    public RaffleAwardEntity performRaffle(RaffleFactorEntity raffleFactorEntity) {
        //1、抽奖参数校验
        Long userId = raffleFactorEntity.getUserId();
        Long strategyId = raffleFactorEntity.getStrategyId();
        if(null == userId || null == strategyId) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        //2、责任链前置规则+抽奖
        ILogicChain logicChain = defaultChainFactory.openLogicChain(raffleFactorEntity.getStrategyId());
        Long awardId = logicChain.logic(raffleFactorEntity.getUserId(), raffleFactorEntity.getStrategyId());
        //3、策略查询
        List<StrategyRuleEntity> strategyRuleEntityList =  strategyRuleRepository.queryStrategyRuleEntityByStrategyId(strategyId);
        List<String> ruleModels = strategyRuleEntityList.stream().map(StrategyRuleEntity::getRuleModel).collect(Collectors.toList());
        // 4、中置过滤
        RuleActionEntity<RuleActionEntity.RaffleCenterEntity> centerRuleActionEntity = this.doCheckRaffleCenterLogic(RaffleFactorEntity.builder().userId(raffleFactorEntity.getUserId()).StrategyId(raffleFactorEntity.getStrategyId()).awardId(awardId).build(), ruleModels);
        if (RuleLogicCheckTypeVO.TAKE_OVER.getCode().equals(centerRuleActionEntity.getCode())) {
            return RaffleAwardEntity.builder()
                    .awardDesc("奖品未解锁，走兜底策略")
                    .build();
        }
        return RaffleAwardEntity.builder()
                .awardId(awardId)
                .build();
    }

    protected abstract RuleActionEntity<RuleActionEntity.RaffleCenterEntity> doCheckRaffleCenterLogic(RaffleFactorEntity raffleFactorEntity, List<String> logics);

}
